package com.csxy.qiu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csxy.qiu.R;
import com.csxy.qiu.entity.EduChapter;
import com.csxy.qiu.entity.EduVideo;
import com.csxy.qiu.entity.vo.ChapterVo;
import com.csxy.qiu.entity.vo.VideoVo;
import com.csxy.qiu.exception.MyException;
import com.csxy.qiu.mapper.EduChapterMapper;
import com.csxy.qiu.service.EduChapterService;
import com.csxy.qiu.service.EduVideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {
   @Autowired
   private EduVideoService eduVideoService;
    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {
        ArrayList<ChapterVo> finalChapterVos = new ArrayList<>();
        //先根据courseId查询所有的章节
        QueryWrapper<EduChapter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        List<EduChapter> chapterList = baseMapper.selectList(queryWrapper);



        //再根据chapterId查询所有的小节
       QueryWrapper<EduVideo> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("course_id",courseId);
        List<EduVideo> videoList = eduVideoService.list(queryWrapper1);
        //拼接章节和小节charpterList
        for (EduChapter chapter : chapterList){
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(chapter,chapterVo);
            finalChapterVos.add(chapterVo);
            List<VideoVo> finalVideoList = new ArrayList<>();
            for(EduVideo video : videoList){
                if(chapter.getId().equals(video.getChapterId())){
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(video,videoVo);
                    finalVideoList.add(videoVo);
                }
                chapterVo.setChildren(finalVideoList);
            }
        }
        return finalChapterVos;
    }



    //根据课程ID删除对应的章节记录
    @Override
    public void removeChapterByCourseId(String id) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",id);
        baseMapper.delete(wrapper);
    }

    //删除章节
    @Override
    public boolean deleteChapter(String chapterId) {
        //根据chapter章节id，查询小节表，如果查询有数据，则不删除
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId);
        int count = (int) eduVideoService.count(wrapper);
        //判断
        if(count > 0){
            //能查询出来小节，不进行删除
            throw new MyException(20001,"该章节下有小节，不能删除");
        }else {
               //不能查询出小节，进行删除
                int delete = baseMapper.deleteById(chapterId);
                return delete>0;
            }
        }

}
